home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
4_12.lha
/
4_12
/
4_12c.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-08
|
3KB
|
160 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
include <stream.h>
include <sys/types.h>
ypedef unsigned short ushort;
include <sys/stat.h>
include <memory.h>
include <ctype.h>
include <fcntl.h>
include <osfcn.h>
nt sizefile(char* filename)
struct stat statbuf;
if (stat(filename, &statbuf) != -1)
return statbuf.st_size;
else
return 0;
oid error(char* msg)
cerr << msg << "\n";
exit(1);
oid readfile(char* filename, int filelen, char* filebfr)
int fd = open(filename, O_RDONLY);
if (fd < 0)
error("cannot open file");
if (read(fd, filebfr, filelen) != filelen)
error("bytes read != size of file");
close(fd);
nt matchcount(char* bufptr1, char* bufptr2)
int ctr = 0;
while (*bufptr1++ == *bufptr2++)
ctr++;
return ctr;
oid dochar(char c)
if (isprint(c))
{
cout.put(c);
cout.put('_');
}
else
cout << form("%2.2x", c);
oid prfactors(int max)
int j = max / 2;
for (int i = 2; i <= j; i++)
if (max % i == 0)
cout << dec(i, 3);
cout << dec(max, 3) << "\n";
oid prperiods(int)
onst int NUMperiods = 150;
truct
int begin;
int length;
periodlist[NUMperiods] = { { -1, 0 } };
oid addperiods(int begin, int interval, int cnt, int filelen)
/* collapse old period info out */
for (int left = 0; periodlist[left].begin != -1; left++)
if (periodlist[left].begin < begin)
{
int right = left + 1;
periodlist[left] = periodlist[right];
for ( ; periodlist[right].begin != -1; )
if (periodlist[++right].begin >= begin)
periodlist[left++] = periodlist[right];
}
/* At this point, periodlist[left].begin == -1. */
/* We now add the new values. */
for (int i = begin + 1; (left < NUMperiods) && (--cnt > 0); )
for (int j = i; (j < filelen) && (left < NUMperiods); j += interval)
{
periodlist[left].begin = j;
periodlist[left++].length = cnt;
}
if (left == NUMperiods)
error("too many periods to add");
nt foundperiod(int begin, int length)
for (int i = 0; (i < NUMperiods) && (periodlist[i].begin != -1); i++)
if ((periodlist[i].begin == begin) &&
(periodlist[i].length == length))
return 1;
return 0;
oid pr_common_periods()
ain(int argc, char **argv)
if (argc != 2)
{
cerr << "usage: " << argv[0] << " encrypted-file\n";
return 1;
}
char* filename = argv[1];
int filelen = sizefile(filename);
char* filebfr = new char[filelen];
readfile(filename, filelen, filebfr);
cout << "length string\n";
cout << "1st pos 2nd pos interval\n";
cout << "factors periods\n\n";
for (int i = 0; i < filelen - 3; i++)
{
for (char* bufptr = &filebfr[i+3];
bufptr = memchr(bufptr,filebfr[i], filelen - (bufptr - filebfr));
bufptr++)
{
int cnt = matchcount(filebfr+i+1, bufptr+1);
if ((cnt > 2) && !foundperiod(i, cnt))
{
int interval = (bufptr - filebfr) - i;
addperiods(i, interval, cnt, filelen);
cout << cnt << " ";
for (char* p = bufptr; cnt--; p++ )
dochar(*p);
cout << "\n" << i << " " << (bufptr - filebfr) << " " <<
interval << "\n";
prfactors(interval);
prperiods(interval);
cout << "\n";
break;
}
}
}
pr_common_periods();
return 0;